package com.example.shop.controller;

import com.alibaba.fastjson.JSON;
import com.example.shop.model.Commodity;
import com.example.shop.model.OrderForm;
import com.example.shop.model.ShopCart;
import com.example.shop.service.OrderService;
import com.example.shop.service.ShopCartService;
import com.example.shop.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ShopCartDataController {
    @Autowired
    private ShopCartService shopCartService;
    @Autowired
    private OrderService orderService;
    private ShopCart shopCart;
    private OrderForm orderForm;

    @RequestMapping("/getShopCart")
    public String getShopCast(int id) {
        List<ShopCart> shopCarts = shopCartService.getShopCarts(id);
        return JSON.toJSONString(shopCarts);
    }

    @RequestMapping("/addShopCart")
    public String addShopCart(int uId, Commodity commodity, String remark, String token) {
        shopCart = new ShopCart();
        Map<String, String> map = new HashMap<>();
        int userId = TokenUtil.getUserId(token);
        ShopCart shopCart = shopCartService.getShopCart(commodity.getId(), userId);
        if (shopCart != null) {
            map.put("message", "购物车内已有此商品");
        } else {
            this.shopCart.setCreated(new Date());
            this.shopCart.setUId(uId);
            this.shopCart.setCommodityId(commodity.getId());
            this.shopCart.setNum(1);
            this.shopCart.setPrice(commodity.getPrice());
            this.shopCart.setSumPrice(commodity.getPrice());
            this.shopCart.setRemark(remark);
            int count = shopCartService.insertShopCart(this.shopCart);
            if (count > 0) {
                map.put("message", "成功");
            } else {
                map.put("message", "添加购物车失败");
            }
        }
        return JSON.toJSONString(map);
    }

    /**
     * 修改方法 进行数量加减
     */
    @RequestMapping("/updateNum")
    public Map<String, String> updateNum(int id, int num, double price) {
        Map<String, String> map = new HashMap<>();
        ShopCart shopCart = new ShopCart();
        shopCart.setId(id);
        shopCart.setNum(num);
        shopCart.setPrice(price);
        shopCart.setSumPrice(num * price);
        int count = shopCartService.updateShopCart(shopCart);
        if (count > 0) {
            map.put("message", "成功");
        } else {
            map.put("message", "失败");
        }
        return map;
    }

    /**
     * deleteShopCart
     */
    @RequestMapping("/delShopCart")
    public String deleteShopCart(int id) {
        Map<String, String> map = new HashMap<>();
        int count = shopCartService.deleteShopCart(id);
        if (count > 0) {
            map.put("message", "成功");
        } else {
            map.put("message", "失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 结账
     */
    @RequestMapping("/billPlease")
    public String billPlease(String idsText, String remark, String token) throws InterruptedException {
        String[] split = idsText.split(",");
        int userId = TokenUtil.getUserId(token);
        orderForm = new OrderForm();
        StringBuffer foodsInfo = new StringBuffer();
        double sumPrice = 0;
        for (String s : split) {
            shopCart = shopCartService.getShopCart(Integer.parseInt(s), userId);
            foodsInfo.append(shopCart.getName()).append(" ").append(shopCart.getPrice()).append("元 * ").append(shopCart.getNum()).append("个<br />");
            sumPrice += shopCart.getPrice() * shopCart.getNum();
        }
        orderForm.setOrders(new String(foodsInfo));
        orderForm.setUId(userId);
        orderForm.setSumPrice(sumPrice);
        orderForm.setCreated(new Date());
        //默认未支付
        orderForm.setStateId(1);
        //备注
        orderForm.setRemark(remark);
        System.err.println(orderForm);
        int count = orderService.insertOrder(orderForm);
        Map<String, String> map = new HashMap<>();
        if (count > 0) {
            map.put("message", "成功");
        } else {
            map.put("message", "失败");
        }
        return JSON.toJSONString(map);
    }
}
